package com.sk.weichat.view;

import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;

import java.util.Arrays;

public class MaxValueInputFilter implements InputFilter {

    public void setOnMaxValueInputListener(OnMaxValueInputListener onMaxValueInputListener) {
        this.onMaxValueInputListener = onMaxValueInputListener;
    }

    private OnMaxValueInputListener onMaxValueInputListener;

    public interface OnMaxValueInputListener {
        void onAllowed(); //允许输入

        void onBaned(); //超出限制禁止输入

        void onIlegal(); //非法
    }

    private float maxValue;

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(dest);
        if (TextUtils.isEmpty(source)) {
            return null;
        } else {
            float number;
            String destStr = dest.toString();
            // Log.e("lzs", "dest:" + dest.toString() + "  source:" + source + "  start:" + start + "  end:" + end + "  dstart:" + dstart + "  dend:" + dend);

            if (destStr.startsWith("0") && !source.toString().equals(".") && !destStr.startsWith("0.")) {
                return "";
            }
            if (!destStr.contains(".") && source.toString().equals(".")) {
                //允许输入小数
                return source;
            } else if (destStr.contains(".") && destStr.indexOf(".") == destStr.length() - 3 && dstart > destStr.indexOf(".")) {
                //限制两位小数
                return "";
            }
            try {
                number = Float.valueOf(source.toString());
                if (dstart != destStr.length()) {
                    spannableStringBuilder.insert(dstart, source, 0, 1);
                } else {
                    spannableStringBuilder.append(source);
                }
                number = Float.valueOf(spannableStringBuilder.toString());
                //Log.e("lzs", "number:" + number);
                if (number > maxValue) {
                    if (null != onMaxValueInputListener) {
                        onMaxValueInputListener.onBaned();
                    }
                    //Log.e("lzs","超出限制");
                    return "";
                } else {
                    //Log.e("lzs","允许范围之内");
                    if (null != onMaxValueInputListener) {
                        onMaxValueInputListener.onAllowed();
                    }
                    return source;
                }
            } catch (Exception e) {
                // Log.e("lzs", "非数字");
                //非数字禁止输入
                if (null != onMaxValueInputListener) {
                    onMaxValueInputListener.onIlegal();
                }
                return "";
            }
        }
    }

    public float getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }
}
